package net.malkuth.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import net.malkuth.entity.QueryEntity;
import net.malkuth.test.AbilityActivity;
import net.malkuth.test.CourseProcess;
import net.malkuth.test.R;

import java.util.List;

/**
 * Created by 一些年华 on 2016/7/8.
 */
public class QueryAdapter extends BaseAdapter {

    private List<QueryEntity> list;
    private Context contex;
    private LayoutInflater layoutInflater;

    public QueryAdapter(List<QueryEntity> list, Context contex) {
        this.layoutInflater = LayoutInflater.from(contex);
        this.list = list;
        this.contex = contex;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHoler viewHoler;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.query_list_item, null);
            viewHoler = new ViewHoler();

            viewHoler.head = (ImageView) convertView.findViewById(R.id.iv_head_image);
            viewHoler.name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHoler.bg_sex = convertView.findViewById(R.id.bg_sex);
            viewHoler.sex = (ImageView) convertView.findViewById(R.id.sex);
            viewHoler.age = (TextView) convertView.findViewById(R.id.tv_age);

            viewHoler.level = (ImageView) convertView.findViewById(R.id.iv_level);
            viewHoler.info1 = (Button) convertView.findViewById(R.id.btn_info1);
            viewHoler.info2 = (Button) convertView.findViewById(R.id.btn_info2);
            viewHoler.info3 = (Button) convertView.findViewById(R.id.btn_info3);
            convertView.setTag(viewHoler);
        } else {
            viewHoler = (ViewHoler) convertView.getTag();
        }

        viewHoler.head.setImageResource(R.drawable.ic_launcher);
        viewHoler.name.setText(list.get(position).getStudent());
        if (list.get(position).getSex() == "男生") {
            viewHoler.bg_sex.setBackgroundResource(R.drawable.bg_man);
            viewHoler.sex.setImageResource(R.drawable.ic_man);
        } else {
            viewHoler.bg_sex.setBackgroundResource(R.drawable.bg_woman);
            viewHoler.sex.setImageResource(R.drawable.ic_woman);
        }

        viewHoler.age.setText(list.get(position).getAge());

        if (list.get(position).getLevel() == "1") {
            viewHoler.level.setImageResource(R.drawable.lv1);
        } else if (list.get(position).getLevel() == "2") {
            viewHoler.level.setImageResource(R.drawable.lv2);
        } else {
            viewHoler.level.setImageResource(R.drawable.lv3);
        }

        viewHoler.info1.setText(list.get(position).getInfo());
        viewHoler.info2.setText(list.get(position).getInfo1());
        viewHoler.info3.setText(list.get(position).getInfo2());

        viewHoler.info1.setBackgroundResource(R.drawable.btn_data_info_normal);
        viewHoler.info2.setBackgroundResource(R.drawable.btn_data_info_normal);


        /*viewHoler.info1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHoler.info1.setBackgroundResource(R.drawable.btn_data_info_press);
            }
        });*/
        viewHoler.info1.setOnClickListener(clickListener);
        viewHoler.info2.setOnClickListener(clickListener);
        viewHoler.info3.setOnClickListener(clickListener);

        return convertView;
    }

    public View.OnClickListener clickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_info1:
                    Toast.makeText(contex, "历史成绩", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn_info2:
                    //viewHoler.info2.setBackgroundResource(R.drawable.btn_data_info_press);
                    Intent intent = new Intent();
                    intent.setClass(contex, CourseProcess.class);
                    contex.startActivity(intent);
                    break;
                case R.id.btn_info3:
                    contex.startActivity(new Intent(contex, AbilityActivity.class));
                    break;
            }
        }
    };


    class ViewHoler {

        private ImageView head;
        private TextView name;
        private View bg_sex;
        private ImageView sex;
        private TextView age;
        private ImageView level;
        private Button info1;
        private Button info2;
        private Button info3;


    }
}
